FROM debian:stretch-slim

LABEL authors="Vincent Dong <dzheng533@gmail.com>"

# |--------------------------------------------------------------------------
# | Required libraries
# |--------------------------------------------------------------------------
# |
# | Installs required libraries.
# |

RUN apt-get update &&\
    apt-get install -y --no-install-recommends curl git nano sudo ca-certificates procps libfontconfig nginx sqlite3 --no-install-recommends

# |--------------------------------------------------------------------------
# | NodeJS
# |--------------------------------------------------------------------------
# |
# | Installs NodeJS and npm.
# |

RUN apt-get update &&\
    apt-get install -y --no-install-recommends gnupg &&\
    curl -sL https://deb.nodesource.com/setup_12.x | bash - &&\
    apt-get update &&\
    apt-get install -y --no-install-recommends nodejs

# |--------------------------------------------------------------------------
# | yarn
# |--------------------------------------------------------------------------
# |
# | Installs yarn. It provides some nice improvements over npm.
# |

RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - &&\
    echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list &&\
    apt-get update &&\
    apt-get install -y --no-install-recommends yarn

# |--------------------------------------------------------------------------
# | PATH updating
# |--------------------------------------------------------------------------
# |
# | Let's add ./node_modules/.bin to the PATH (utility function to use NPM bin easily)
# |

ENV PATH="$PATH:./node_modules/.bin"
RUN sed -i 's#/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin#/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:./node_modules/.bin#g' /etc/sudoers


RUN mkdir -p /app/nginx/config && \
    mkdir -p /app/nginx/logs && \
    mkdir /app/docker/ && \
    mkdir -p /app/server/data

COPY ./dockers/docker-entrypoint.sh /app/docker/docker-entrypoint.sh

RUN chmod +x /app/docker/docker-entrypoint.sh
# install app
ENV DATA_ROOT=/app/server/data WEB_PORT=3000
VOLUME ${DATA_ROOT}
VOLUME /app/nginx/logs
WORKDIR /app/server/

COPY ./ /app/server/

RUN yarn install \
  && mkdir -p ${DATA_ROOT}/storage \
  && mkdir -p ${DATA_ROOT}/database \
  && export DATA_ROOT=${DATA_ROOT} \
  && export PORT=${WEB_PORT}} \
  && mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak \
  && cp dockers/nginx.conf /etc/nginx/nginx.conf
  
EXPOSE ${WEB_PORT}
EXPOSE 80

## CMD ["yarn run start"]

ENTRYPOINT ["/app/docker/docker-entrypoint.sh"]